package com.keke.study_app;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

import androidx.annotation.NonNull;

public class MainActivity extends FlutterActivity {
    private static final String CHANNEL = "methodChannel";

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        DartExecutor dartExecutor = flutterEngine.getDartExecutor();
        MethodChannel channel = new MethodChannel(dartExecutor, CHANNEL);
        channel.setMethodCallHandler(callHandler);
    }

    private MethodChannel.MethodCallHandler callHandler = (call, result) -> {
        if (call.method.equals("startRead")) {
            startRead(call);
        }
        if (call.method.equals("stopRead")) {
            stopRead(call);
        }
    };

    private void startRead(MethodCall call) {
        if (call.arguments instanceof String) {
            MyReader myReader = MyReader.getInstance();
            myReader.speak(MainActivity.this, (String) call.arguments);
        }
    }

    private void stopRead(MethodCall call) {
        MyReader myReader = MyReader.getInstance();
        myReader.stop();
    }


}
